<template>
    <a-modal
        v-model:visible="showModal"
        :title="title"
        :width="900"
        :destroyOnClose="true"
        :maskClosable="false"
        :confirmLoading="loading"
        @ok="onSubmit"
        @cancel="handleCancle"
    >
        <a-form
            class="dept-model"
            layout="horizontal"
            :model="formState"
            :rules="rules"
            ref="formRef"
            style="justify-content: space-between"
        >
            <div>
                <!-- 所属商户 -->
                <a-form-item class="full-width" :label="t('yl.merchantLabelName')" name="name">
                    <a-input v-model:value="formState.name" :placeholder="t('yl.merchantLabelName')" />
                </a-form-item>
                <!-- 选择区域 -->
                <a-form-item class="full-width" :label="t('yl.areaCode')" name="areaCode">
                    <!-- <a-input v-model:value="formState.areaCode" :placeholder="t('yl.areaCode')" /> -->

                    <a-tree-select
                        v-model:value="formState.areaCode"
                        style="width: 100%"
                        :tree-data="treeData"
                        :replaceFields="{ title: 'areaName', key: 'id', value: 'areaCode', children: 'children' }"
                        allow-clear
                        :show-checked-strategy="'SHOW_PARENT'"
                        :dropdownClassName="'yl-areaCode'"
                        :placeholder="t('yl.areaCode')"
                    />
                </a-form-item>
                <!-- 负责专员 -->
                <a-form-item class="full-width" :label="t('yl.afterSalesSpecialis')" name="afterSalesSpecialis">
                    <a-input v-model:value="formState.afterSalesSpecialis" :placeholder="t('yl.afterSalesSpecialis')" />
                </a-form-item>
                <!-- 收银编号 -->
                <a-form-item class="full-width" :label="t('yl.cashRegisterName')" name="cashRegister">
                    <a-input v-model:value="formState.cashRegister" :placeholder="t('yl.cashRegister')" />
                </a-form-item>
                <!-- 账号 -->
                <a-form-item class="full-width" :label="t('yl.account')" name="account">
                    <a-input v-model:value="formState.account" :placeholder="t('yl.account')" />
                </a-form-item>

                <!-- 密码 -->
                <a-form-item class="full-width" :label="t('yl.password')" name="password" v-if="!formState.id">
                    <a-input v-model:value="formState.password" :placeholder="t('yl.password')" />
                </a-form-item>
            </div>
            <div>
                <!-- 邮箱 -->
                <a-form-item class="full-width" :label="t('yl.email')" name="email">
                    <a-input v-model:value="formState.email" :placeholder="t('yl.email')" />
                </a-form-item>

                <!-- 联系人 -->
                <a-form-item class="full-width" :label="t('yl.contactName')" name="contactName">
                    <a-input v-model:value="formState.contactName" :placeholder="t('yl.contactName')" />
                </a-form-item>

                <!-- 手机号 -->
                <a-form-item class="full-width" :label="t('yl.contactTel')" name="contactTel">
                    <a-input v-model:value="formState.contactTel" :placeholder="t('yl.contactTel')" />
                </a-form-item>

                <!-- 省份 -->
                <a-form-item class="full-width" :label="t('yl.provinceNameLabel')" name="provinceName">
                    <a-cascader
                        :fieldNames="{ label: 'name', value: 'name', children: 'countryRegionDtos' }"
                        :options="countryRegionList"
                        :placeholder="t('yl.provinceNameLabel')"
                        v-model:value="formState.provinceList"
                    />
                </a-form-item>
                <!-- 详细地址 -->
                <a-form-item class="full-width" :label="t('yl.address')" name="address">
                    <a-input v-model:value="formState.address" :placeholder="t('yl.address')" />
                </a-form-item>
            </div>
            <!-- <div>
               
                <a-checkbox-group v-model:value="value">
                    <div class="power">
                        <a-checkbox value="A">引用总部商品</a-checkbox>
                        <span>总部管理控价引用的商品；</span>
                    </div>
                    <div class="power">
                        <a-checkbox value="B">允许自建商品</a-checkbox>
                        <span>门店管理控价本店自建的商品；</span>
                    </div>
                </a-checkbox-group>
            </div> -->
        </a-form>
    </a-modal>
</template>
<script lang="ts">
import { computed, defineComponent, reactive, ref, watch, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import _ from 'lodash';
import ylTreeselectVue from '@/components/ylTreeselect.vue';
import { getMerchantPageData, getAreaTree } from '@/api/merchant';

import { getCountryRegion } from '@/api/region';

interface DeptState {
    password: string | number;
    contactName: string | number;
    cashRegister: string | number;
    account: string | number;
    contactTel: string | number;
    email: string | number;
    address: string | number;
    afterSalesSpecialis: string | number;
    provinceList: any[];
    areaCode: string | number;
    name: string;
    provinceName: string | number;
    provinceId: string | number;
    cityName: string | number;
    cityId: string | number;
    countyName: string | number;
    countyId: string | number;
    areaName: string | number;
    areaId: string | number;
    systype: string | number;
    id: string | number;
}

// 表单校验规则
const initRules = (t: fn) => {
    return {
        password: [{ required: true, message: t('yl.password'), trigger: 'blur' }],
        name: [{ required: true, message: t('yl.storeName'), trigger: 'blur' }],
        account: [{ required: true, message: t('yl.account'), trigger: 'blur' }],
        email: [{ required: true, message: t('yl.email'), trigger: 'blur' }],
        contactName: [{ required: true, message: t('yl.contactName'), trigger: 'blur' }],
        contactTel: [{ required: true, message: t('yl.contactTel'), trigger: 'blur' }],
    };
};

export default defineComponent({
    name: 'roleModel',
    components: {
        ylTreeselectVue,
    },
    props: {
        visible: {
            type: Boolean,
            default: () => false,
        },

        title: {
            type: String,
        },
        loading: {
            type: Boolean,
            default: () => false,
        },
        dept: {
            type: Object,
        },
        countryRegionList: {
            type: Array,
        },
    },
    emits: ['submit', 'cancle'],
    setup(props, { emit }) {
        const { t } = useI18n();
        const showModal = computed(() => props.visible);
        const countryRegionList = ref<any>([]);
        const treeData = ref<any>([]);
        const formState: DeptState = reactive({
            name: '',
            areaCode: '',
            afterSalesSpecialis: '',
            cashRegister: '',
            password: '', // 设备编号
            account: '',
            email: '',
            contactName: '',
            contactTel: '',
            // 省份
            provinceList: [],
            address: '',
            provinceName: '',
            provinceId: '',
            cityName: '',
            cityId: '',
            countyName: '',
            countyId: '',
            areaName: '',
            areaId: '',
            systype: '',
            id: '',
        });

        const {
            name,
            areaCode,
            afterSalesSpecialis,
            cashRegister,
            contactName,
            account,
            contactTel,
            address,
            email,
            provinceName,
            provinceId,
            cityName,
            cityId,
            countyName,
            countyId,
            areaName,
            areaId,
            id,
        } = props.dept as NormalObj;
        formState.areaCode = areaCode;
        formState.name = name;
        formState.contactName = contactName;
        formState.account = account;
        formState.contactTel = contactTel;
        formState.address = address;
        formState.afterSalesSpecialis = afterSalesSpecialis;
        formState.email = email;
        formState.cashRegister = cashRegister;
        formState.id = id;
        if (areaName) {
            formState.provinceList = (provinceName + ',' + cityName + ',' + countyName + ',' + areaName).split(',');
        } else if (countyName) {
            formState.provinceList = (provinceName + ',' + cityName + ',' + countyName).split(',');
        } else if (provinceName && cityName) {
            formState.provinceList = (provinceName + ',' + cityName).split(',');
        } else {
            formState.provinceList = [];
        }
        formState.systype = 'D';
        console.log(props.countryRegionList);
        countryRegionList.value = props.countryRegionList;

        onMounted(() => {
            handleAreaTree();
        });

        // 获取选择区域数据
        const handleAreaTree = async () => {
            treeData.value = await getAreaTree();
        };
        // 表单校验
        const rules = initRules(t);
        const formRef = ref();
        // 提交
        const onSubmit = () => {
            formRef.value.validate().then(() => {
                let pathList: any = [];
                if (formState.provinceList.length) {
                    TreeFun(countryRegionList.value, pathList);
                    if (pathList.length) {
                        formState.provinceName = pathList[0].name;
                        formState.provinceId = pathList[0].id;
                        formState.cityName = pathList[1].name;
                        formState.cityId = pathList[1].id;
                        if (pathList.length == 3) {
                            formState.countyName = pathList[2].name;
                            formState.countyId = pathList[2].id;
                        } else if (pathList.length == 4) {
                            formState.areaName = pathList[3].name;
                            formState.areaId = pathList[3].id;
                        }
                    }
                }
                emit('submit', formState);
            });
        };
        // 取消
        const handleCancle = () => {
            return emit('cancle');
        };
        const TreeFun = (list: any, pathList: any) => {
            for (let i = 0; i < list.length; i++) {
                for (let j = 0; j < formState.provinceList.length; j++) {
                    if (list[i].name == formState.provinceList[j]) {
                        pathList.push({ id: list[i].id, name: list[i].name });
                        if (list[i].countryRegionDtos && list[i].countryRegionDtos.length) {
                            TreeFun(list[i].countryRegionDtos, pathList);
                        }
                    }
                }
            }
        };

        return {
            formRef,
            showModal,
            formState,
            t,
            rules,
            onSubmit,
            handleCancle,
            countryRegionList,
            treeData,
            handleAreaTree,
        };
    },
});
</script>
<style lang="scss" scoped>
.dept-model {
    @include flexWrap;
    width: 100%;
    .ant-form-item {
        @include flex;
        width: 50%;
        margin-right: 0;
        padding-right: $width10;
        margin-bottom: $width8;
        :deep .ant-form-item-label {
            flex-basis: $formLabel;
        }
        :deep .ant-form-item-control-wrapper {
            flex: 1;
        }
        :deep .ant-form-item-children,
        :deep .ant-input-number {
            width: 100%;
        }
        &.full-width {
            width: 100%;
        }
    }
    .power {
        width: 210px;

        border: 1px solid #ff6702;
        border-radius: 5px;
        margin-top: 15px;
        padding: 15px 5px;

        > span {
            display: block;
            width: 100%;
        }
        .ant-checkbox-wrapper {
            color: #ff6702;
            font-size: 14px;
        }
    }
}
</style>
